<?php

namespace App\Events;

use Illuminate\Broadcasting\Channel;
use Illuminate\Queue\SerializesModels;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;

class DeviceError implements ShouldBroadcast
{
    use Dispatchable, InteractsWithSockets, SerializesModels;

    /**
     * 设备编号
     * @var
     */
    public $device_sn;

    /**
     * 错误消息
     * @var
     */
    public $error_msg;

    /**
     * Create a new event instance.
     * @param $device_sn
     * @param $error_msg
     */
    public function __construct($device_sn, $error_msg)
    {
        $this->device_sn = $device_sn;
        $this->error_msg = $error_msg;
    }

    /**
     * Get the channels the event should broadcast on.
     *
     * @return \Illuminate\Broadcasting\Channel|array
     */
    public function broadcastOn()
    {
        return new Channel('device-error.' . $this->device_sn);
    }

    /**
     * 广播数据
     * @return array
     */
    public function broadWith()
    {
        return [
            'ports' => [ 'p1' => 0, 'p2' => 0, 'p3' => 0, 'p4' => 0, 'p5' => 0, 'p6' => 0],
            'status' => 'fault',
            'status_text' => '设备故障'
        ];
    }
}
